#pragma once
#ifndef __ROUTERCONTROLSERVER_H__
#define __ROUTERCONTROLSERVER_H__

#include <app/App.h>
#include <memory>
#include <net/tcp_server.hpp>
#include <util/DataMap.hpp>
#include <util/forward_table.hpp>
#include <util/output_forward_table.hpp>
#include <util/singleton.hpp>
#include <router/RouterResponse.h>
#include <router/RouterRequest.h>


// 守护进程  app_Id 应该是0或1
class RouterControlServer: public tcp_server, public App, public std::enable_shared_from_this<RouterControlServer>
{
    forward_table   forwardtable;
    output_forward_table output_forwardtable;
    DataMap<uint32_t, response> received_response;

    static const int timeout_sec = 5;
public:
    RouterControlServer(uint16_t port);

    std::pair<response,bool> get_response(uint32_t req_id, int timeout_sec);

    // message_hander处理的是 其他应用、路由服务器发送过来的消息，一般是命令  [不对]
    // 处理的应该是router的response [不对， reponse自己主动拉去]
    virtual void message_hander(message msg) override;

    virtual void fd_hander() override;

    bool login(uint32_t user, uint32_t host, std::string_view password);

    void notify_new_client_connect(std::shared_ptr<VirtualServer> server, std::shared_ptr<TrueClient> client);

    void notify_true_client_disconnect(std::shared_ptr<TrueClient> client);

    void notify_virtual_client_disconnect(std::shared_ptr<VirtualClient> client);

    response hand_request(request req);
};

// #define routerctl_init(port) \
// template<> \
// inline RouterControlServer EagerSingleton<RouterControlServer>::instance = RouterControlServer(8970);

// #define routerctl EagerSingleton<RouterControlServer>::getInstance()

#endif // __ROUTERCONTROLSERVER_H__